<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
		<script>
			"use strict"
			
			// for in 循环对象(属性名) 数组(下标)
			let obj ={username: "kobe", age: 39};
			for(let i in obj){
				console.log(i);
			}
			let arr = [1,3,"abc"]
			for(let i in arr){
				console.log(i);
			}
			
			// 定义检测数据类型的功能函数
			function checkedType(target){
				return Object.prototype.toString.call(target).slice(8, -1);
			}
			
			// 实现深度克隆 ---> 对象/数组
			function clone(target){
				// 判断拷贝的数据类型
				let result, targetType = checkedType(target);
				if(targetType === "Object"){
					result = {};
				}else if(targetType === "Array"){
					result = [];
				}else{
					return target;
				}
				
				// 遍历目标数据
				for(let i in target){
					// 获取遍历数据结构的每一项值
					let value = target[i];
					// 判断目标结构里的每一值是否存在对象
					if(checkedType(value) === "Object" || checkedType(value) === "Array"){
						// 继续遍历获取到的value的值
						result[i] = clone(value);
					}else{
						result[i] = value;
					}
				}
				return result;
			}
			
			let arr1 = [1, 2, true, {username: "kobe"}, "女"];
			let arr2 = clone(arr1);
			arr1[3].username = "damo";
			console.log("arr1", arr1);
			console.log("arr2", arr2);
			
			let obj1 = {username: "kobe", age: 39};
			let obj2 = clone(obj);
			obj1.username = "damo"
			console.log("obj1", obj1);
			console.log("obj2", obj2);
		</script>
		
	</body>
</html>
